#include "Common.h"
#include "Sock/AppLibSock_Unix.hpp"

#define IGNORE_RET(func) (void)(func)

void Flush(void)
{
    char c;

    while((c = getchar()) != '\n' && c != EOF);
}

int main (int argc, char **args)
{
	int iRet = 0;
	char buf[256] = {0};
	ULONG uRet = STAT_SUCCESS;
	APP_LIB_SOCK_s stSock;

	if (argc != 3)
	{
		printf("Usage: ./unixSockCli <type [0:stream| 1: dgram]> <srv path>\n");
		return -1;
	}

	if (atoi(args[1]) != APP_LIB_SOCKET_TYPE_UNIX_STREAM && atoi(args[1]) != APP_LIB_SOCKET_TYPE_UNIX_DGRAM)
	{
		printf("Usage: ./unixSockSrv <type [0:stream| 1: dgram]> <Path>\n");
		return -1;
	}

	memset(&stSock, 0, sizeof(stSock));
	stSock.enType = atoi(args[1]);
	uRet = AppLibSockUnix_CliCreate(stSock.enType, args[2], &stSock);
	if (uRet != STAT_SUCCESS)
	{
		return -2;
	}

    if (stSock.enType == APP_LIB_SOCKET_TYPE_UNIX_STREAM)
	{
        uRet = AppLibSock_Connect(&stSock);
        if (uRet != STAT_SUCCESS)
        {
            AppLibSock_Close(&stSock);
            return uRet;
        }
    }

	while (1)
	{
		memset(buf, 0, sizeof(buf));

		iRet = scanf("%s", buf);
        UNUSED(iRet);
		Flush();

		iRet = AppLibSock_Send(&stSock, buf, strlen(buf), 0);

		printf("-------iRet [%d], send [%s]\n", iRet, buf);

		if (strcmp(buf, "quit") == 0)
		{
			break;
		}
	}

	AppLibSock_Close(&stSock);

	return 0;
}

